package com.googlecode.spruce.remote.listener;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;

import com.googlecode.spruce.core.ServiceCheckPluginResult;

/**
 * A {@link ServiceCheckListener} that will delegate each call to
 * {@link #preServiceCheck(String)} and
 * {@link #postServiceCheck(String, ServiceCheckPluginResult)} to a list of
 * delegate {@link ServiceCheckListener}s
 */
public final class DelegatingServiceCheckListener implements ServiceCheckListener
{
    private final Collection<ServiceCheckListener> delegates;

    /**
     * Default Constructor
     */
    public DelegatingServiceCheckListener()
    {
        this.delegates = new ArrayList<ServiceCheckListener>();
    }

    /**
     * Called immediately before a service check plugin is executed
     * 
     * @param action
     *            The name of the plugin that will be invoked
     */
    @Override
    public void preServiceCheck(final String action)
    {
        for (final ServiceCheckListener delegate : this.delegates)
        {
            delegate.preServiceCheck(action);
        }
    }

    /**
     * Called immediately after a service check plugin is executed
     * 
     * @param action
     *            The name of the plugin that will be invoked
     * @param result
     *            The result of that plugin
     */
    @Override
    public void postServiceCheck(final String action, final ServiceCheckPluginResult result)
    {
        for (final ServiceCheckListener delegate : this.delegates)
        {
            delegate.postServiceCheck(action, result);
        }
    }

    /**
     * @return the delegates
     */
    public Collection<ServiceCheckListener> getDelegates()
    {
        return Collections.unmodifiableCollection(delegates);
    }

    /**
     * @param delegates
     *            the delegates to set
     */
    public void setDelegates(final Collection<ServiceCheckListener> delegates)
    {
        if (delegates != null)
        {
            this.delegates.addAll(delegates);
        }
    }

}
